package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingBaseSupplierGroupEntity;
import com.ly.heart.mapper.KingBaseSupplierGroupMapper;
import com.ly.heart.service.IKingBaseSupplierGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-供应商分组信息Service业务层处理
 *
 * @author cxl
 * @date 2024-06-20
 */
@Service
public class KingBaseSupplierGroupServiceImpl extends ServiceImpl<KingBaseSupplierGroupMapper, KingBaseSupplierGroupEntity> implements IKingBaseSupplierGroupService {

    @Resource
    private KingBaseSupplierGroupMapper kingBaseSupplierGroupMapper;

    /**
     * 查询金蝶-供应商分组信息
     *
     * @param id 金蝶-供应商分组信息ID
     * @return 金蝶-供应商分组信息
     */
    @Override
    public KingBaseSupplierGroupEntity selectKingBaseSupplierGroupEntityById ( String id ) {
        return kingBaseSupplierGroupMapper.selectKingBaseSupplierGroupEntityById ( id );
    }

    /**
     * 查询金蝶-供应商分组信息列表
     *
     * @param kingBaseSupplierGroupEntity 金蝶-供应商分组信息
     * @return 金蝶-供应商分组信息
     */
    @Override
    public AjaxResult selectKingBaseSupplierGroupEntityList ( KingBaseSupplierGroupEntity kingBaseSupplierGroupEntity ) {
        Page<KingBaseSupplierGroupEntity> page = this.page ( new Page<> ( kingBaseSupplierGroupEntity.getPage (), kingBaseSupplierGroupEntity.getLimit () ), new LambdaQueryWrapper<KingBaseSupplierGroupEntity> ().eq ( KingBaseSupplierGroupEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        return AjaxResult.success ( page );
    }

    /**
     * 新增金蝶-供应商分组信息
     *
     * @param kingBaseSupplierGroupEntity 金蝶-供应商分组信息
     * @return 结果
     */
    @Override
    public int insertKingBaseSupplierGroupEntity ( KingBaseSupplierGroupEntity kingBaseSupplierGroupEntity ) {
        kingBaseSupplierGroupEntity.setCreateTime ( DateUtils.getNowDate () );
        int count = this.count ();
        kingBaseSupplierGroupEntity.setCode ( "GYSFZ".concat ( String.format ( "%08d", count ) ) );
        kingBaseSupplierGroupEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingBaseSupplierGroupEntity.setUserId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        return kingBaseSupplierGroupMapper.insertKingBaseSupplierGroupEntity ( kingBaseSupplierGroupEntity );
    }

    /**
     * 修改金蝶-供应商分组信息
     *
     * @param kingBaseSupplierGroupEntity 金蝶-供应商分组信息
     * @return 结果
     */
    @Override
    public int updateKingBaseSupplierGroupEntity ( KingBaseSupplierGroupEntity kingBaseSupplierGroupEntity ) {
        kingBaseSupplierGroupEntity.setChangeTime ( DateUtils.getNowDate () );
        return kingBaseSupplierGroupMapper.updateKingBaseSupplierGroupEntity ( kingBaseSupplierGroupEntity );
    }

    /**
     * 批量删除金蝶-供应商分组信息
     *
     * @param ids 需要删除的金蝶-供应商分组信息ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseSupplierGroupEntityByIds ( String[] ids ) {
        List<KingBaseSupplierGroupEntity> list = this.list (new LambdaQueryWrapper<KingBaseSupplierGroupEntity> ().in ( KingBaseSupplierGroupEntity::getId, ids ));
        List<String> idList = list.stream ().filter ( group -> group.getIsDel () == 1 ).map ( KingBaseSupplierGroupEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( group -> {
            if ( group.getIsDel () == 2 ) {
                stringBuilder.append ( group.getName () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseSupplierGroupEntity> ().
                set ( KingBaseSupplierGroupEntity :: getChangeTime, DateUtils.getNowDate () ).
                set ( KingBaseSupplierGroupEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                in ( KingBaseSupplierGroupEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除供应商分组失败！" );
        }
        String str = "删除结供应商分组成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-供应商分组信息信息
     *
     * @param id 金蝶-供应商分组信息ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled ( String id ) {
        KingBaseSupplierGroupEntity byId = this.getById ( id );
        if ( byId == null ) {
            throw new CustomException ( "无此供应商分组！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseSupplierGroupEntity> ().
                set ( KingBaseSupplierGroupEntity :: getChangeTime, DateUtils.getNowDate () ).
                set ( KingBaseSupplierGroupEntity :: getIsDisabled, byId.getIsDisabled () == 1 ? 2 : 1 ).
                eq ( KingBaseSupplierGroupEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( byId.getIsDisabled () == 1 ? "禁用供应商分组失败！" : "解禁" );
        }
        return AjaxResult.success ();
    }
}
